package com.aghcampusnavigation.navi;

import android.app.Fragment;
import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.content.Context;

import java.io.IOException;
import java.lang.reflect.Array;
import java.sql.SQLException;
import android.database.sqlite.SQLiteException;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.aghcampusnavigation.navi.model.BuildingsArrayAdapter;

/**
 * Created by Filip on 20.04.14.
 */
public class DestinationChooserFragment extends ListFragment {

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        SQLliteDatabaseHelper myDbHelper= new SQLliteDatabaseHelper(l.getContext());

        try {
            myDbHelper.openDataBase();
        } catch(SQLException e){
            throw new Error("Unable to open database");
        }
        myDbHelper.addCount((DataOfBuilding) getListAdapter().getItem(position));
        myDbHelper.close();

        Intent intent = new Intent(v.getContext(), AGHMap.class);
        Bundle b = new Bundle();
        try {
            DataOfBuilding d = (DataOfBuilding) getListAdapter().getItem(position);

            b.putString("name", d.gettingNumber());
            b.putFloat("latitude", d.gettingLatitude());
            b.putFloat("longitude", d.gettingLongitude());
            b.putString("desc", d.gettingDescription());
            intent.putExtras(b);
            startActivity(intent);
        } catch (NullPointerException e) {
            e.printStackTrace();
        }

//        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=" + "Miechowska+17,+Kraków" + "&daddr=" + "Reymonta+13,+Kraków"));
//        intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
//        startActivity(intent);
    }

    public static DestinationChooserFragment newInstance(Context context, int position) {
        DestinationChooserFragment f = new DestinationChooserFragment();

        SQLliteDatabaseHelper myDbHelper= new SQLliteDatabaseHelper(context);

        DataOfBuilding[] aghBuildings = new DataOfBuilding[32];
        DataOfBuilding[] dorms = new DataOfBuilding[51-aghBuildings.length];

        try {
            myDbHelper.createDataBase();
        } catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {
            myDbHelper.openDataBase();
        } catch(SQLException e){
            throw new Error("Unable to open database");
        }

        DataOfBuilding[] values1 = new DataOfBuilding[] {};
        switch(position) {
            case 0:
                Log.d("FAVORITES", "getting favorites");
                values1 = myDbHelper.getFavorites();
                break;
            case 1:
                for(int i = 0; i < aghBuildings.length; i++) {
                    aghBuildings[i] = myDbHelper.getBuilding(i+1);
                }
                values1 = aghBuildings;
                break;
            case 2:
                for(int i = 0; i < dorms.length; i++) {
                    dorms[i] = myDbHelper.getBuilding(i+1+aghBuildings.length);
                }
                values1 = dorms;
                break;
            default:
                break;
        }
        myDbHelper.close();
//        adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, values);

        BuildingsArrayAdapter adapter1;
        adapter1 = new BuildingsArrayAdapter(context, values1);

        f.setListAdapter(adapter1);
        return f;
    }

}
